home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gmp-132.lha / gmp-1.3.2 / mpz_sqrt.c < prev    next >
C/C++ Source or Header  |  1993-05-19  |  2KB  |  88 lines

  1. /* mpz_sqrt(root, u) --  Set ROOT to floor(sqrt(U)).
  2.  
  3. Copyright (C) 1991 Free Software Foundation, Inc.
  4.  
  5. This file is part of the GNU MP Library.
  6.  
  7. The GNU MP Library is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. The GNU MP Library is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with the GNU MP Library; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* This code is just correct if "unsigned char" has at least 8 bits.  It
  22.    doesn't help to use CHAR_BIT from limits.h, as the real problem is
  23.    the static arrays.  */
  24.  
  25. #include "gmp.h"
  26. #include "gmp-impl.h"
  27.  
  28. void
  29. #ifdef __STDC__
  30. mpz_sqrt (MP_INT *root, const MP_INT *op)
  31. #else
  32. mpz_sqrt (root, op)
  33.      MP_INT *root;
  34.      const MP_INT *op;
  35. #endif
  36. {
  37.   mp_size op_size, root_size;
  38.   mp_ptr root_ptr, op_ptr;
  39.   mp_ptr free_me = NULL;
  40.   mp_size free_me_size;
  41.  
  42.   op_size = op->size;
  43.   if (op_size < 0)
  44.     op_size = 1 / op_size > 0;    /* Divide by zero for negative OP.  */
  45.  
  46.   /* The size of the root is accurate after this simple calculation.  */
  47.   root_size = (op_size + 1) / 2;
  48.  
  49.   root_ptr = root->d;
  50.   op_ptr = op->d;
  51.  
  52.   if (root->alloc < root_size)
  53.     {
  54.       if (root_ptr == op_ptr)
  55.     {
  56.       free_me = root_ptr;
  57.       free_me_size = root->alloc;
  58.     }
  59.       else
  60.     (*_mp_free_func) (root_ptr, root->alloc * BYTES_PER_MP_LIMB);
  61.  
  62.       root->alloc = root_size;
  63.       root_ptr = (mp_ptr) (*_mp_allocate_func) (root_size * BYTES_PER_MP_LIMB);
  64.       root->d = root_ptr;
  65.     }
  66.   else
  67.     {
  68.       /* Make OP not overlap with ROOT.  */
  69.       if (root_ptr == op_ptr)
  70.     {
  71.       /* ROOT and OP are identical.  Allocate temporary space for OP.  */
  72.       op_ptr = (mp_ptr) alloca (op_size * BYTES_PER_MP_LIMB);
  73.       /* Copy to the temporary space.  Hack: Avoid temporary variable
  74.        by using ROOT_PTR.  */
  75.       MPN_COPY (op_ptr, root_ptr, op_size);
  76.     }
  77.     }
  78.  
  79.   mpn_sqrt (root_ptr, NULL, op_ptr, op_size);
  80.  
  81.   root->size = root_size;
  82.  
  83.   if (free_me != NULL)
  84.     (*_mp_free_func) (free_me, free_me_size * BYTES_PER_MP_LIMB);
  85.  
  86.   alloca (0);
  87. }
  88.